﻿using System;
using System.Diagnostics;
using System.Windows;

namespace NB_Controller_Test
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            // Get Reference to the current Process
            Process thisProc = Process.GetCurrentProcess();
            // Check how many total processes have the same name as the current one
            if (Process.GetProcessesByName(thisProc.ProcessName).Length > 1)
            {
                // If ther is more than one, than it is already running.
                MessageBox.Show("程序已经启动了，请确认！", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
                Application.Current.Shutdown();
                return;
            }

            //捕获所有位置的异常，不至于崩溃
            DispatcherUnhandledException += App_DispatcherUnhandledException;

            base.OnStartup(e);
        }

        private void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            var comException = e.Exception as System.Runtime.InteropServices.COMException;

            //0x800401D0 //OpenClipboard HRESULT:0x800401D0 (CLIPBRD_E_CANT_OPEN))
            if (comException != null && (comException.ErrorCode == -2147221040 /*|| comException.ErrorCode == 0x800401D0*/))
            {
                e.Handled = true;
                return;
            }               


            //使用FileVersionInfo
            string filePath = System.Windows.Forms.Application.ExecutablePath;
            var versionInfo = System.Diagnostics.FileVersionInfo.GetVersionInfo(filePath);
            var FileName = versionInfo.FileName;                    //"F:\\WPF实例\\bin\\Debug\\WPF实例.EXE"
            //var FileDescription = versionInfo.FileDescription;      //"WPF实例"
            //var ProductName = versionInfo.ProductName;              //"WPF实例"
            //var CompanyName = versionInfo.CompanyName;              //"Micro"
            //var FileVersion = versionInfo.FileVersion;              //"5.6.7.8"
            //var ProductVersion = versionInfo.ProductVersion;        //"5.6.7.8"
            //var ProductMajorPart = versionInfo.ProductMajorPart;    //5
            //var ProductMinorPart = versionInfo.ProductMinorPart;    //6
            //var ProductBuildPart = versionInfo.ProductBuildPart;    //7
            //var ProductPrivatePart = versionInfo.ProductPrivatePart;//8
            //// 通常版本号显示为「主版本号.次版本号.生成号.专用部件号」
            //var Version = String.Format("{0}.{1}.{2}.{3}", ProductMajorPart, ProductMinorPart, ProductBuildPart, ProductPrivatePart);
            //var Language = versionInfo.Language;                    //"语言中性"
            var OriginalFilename = versionInfo.OriginalFilename;    //"WPF实例.exe"
            //var LegalCopyright = versionInfo.LegalCopyright;        //"Copyright ©  2018"

            string strErrorInfo = "程序发生了异常，已被捕获！\r\n描述：" + e.Exception.Message
                + "\r\n详细: " + Environment.NewLine + e.Exception.StackTrace
                + "\r\n异常方法: " + Environment.NewLine + e.Exception.TargetSite
                + "\r\nHelpLink: " + Environment.NewLine + e.Exception.HelpLink
                //+ "\r\nInnerException.HelpLink: " + Environment.NewLine + e.Exception.InnerException.HelpLink
                //+ "\r\nInnerException.StackTrace: " + Environment.NewLine + e.Exception.InnerException.StackTrace
                + "\r\n \r\n解决办法：在任务管理器中找到【" + OriginalFilename + "】，"//e.Exception.Source
                + "\r\n右键单击【创建转储文件】菜单，"
                + "\r\n创建完毕后根据提示找到此文件，发送给软件作者。";

            System.Diagnostics.Debug.WriteLine(strErrorInfo);

            //LogHelper.WriteErrorLog(typeof(App), strErrorInfo);
            //LogHelper.WriteExceptionLog(typeof(App), e.Exception);

            //不再报错，改为内部记录
            MessageBox.Show(strErrorInfo, "提示", MessageBoxButton.OK, MessageBoxImage.Information);

            e.Handled = true;
        }
    }
}
